package com.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class TopicMQConfig 
{
	@Value("${rabbitmq.queue.topic1}")
	String queueName1;
	
	@Value("${rabbitmq.queue.topic2}")
	String queueName2;

	@Value("${rabbitmq.exchange.topic}")
	String exchangeKey;

	@Value("${rabbitmq.routing.topic}")
	String routingKey;

	@Bean(value = "${rabbitmq.queue.topic1}")
	public Queue initOrderCancelQueue1(){
	    return new Queue(queueName1) ;
	}
	
	@Bean(value = "${rabbitmq.queue.topic2}")
	public Queue initOrderCancelQueue2(){
	    return new Queue(queueName2) ;
	}

	@Bean(value = "${rabbitmq.exchange.topic}")
	public TopicExchange topicExchange(){
	    return new TopicExchange (exchangeKey);
	}

	@Bean
	public Binding bindingA(@Qualifier(value = "${rabbitmq.queue.topic1}") Queue queue1,
	                       @Qualifier(value = "${rabbitmq.exchange.topic}") TopicExchange  exchange){
	    return BindingBuilder
	            .bind(queue1)
	            .to(exchange)
	            .with(routingKey+".*");
	}
	
	@Bean
	public Binding bindingB(@Qualifier(value = "${rabbitmq.queue.topic2}") Queue queue2,
	                       @Qualifier(value = "${rabbitmq.exchange.topic}") TopicExchange  exchange){
	    return BindingBuilder
	            .bind(queue2)
	            .to(exchange)
	            .with(routingKey+".#");
	}
}
